package cz.muni.fi.pa165.dundd.web;

import cz.muni.fi.pa165.dundd.dto.AdministratorDTO;
import cz.muni.fi.pa165.dundd.service.AdministratorService;
import java.util.List;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author xszanto1, xkonval
 */
@UrlBinding("/admins/{$event}/{admin.id}")
public class AdminActionBean extends BaseActionBean implements ValidationErrorHandler {

    final static Logger log = LoggerFactory.getLogger(AdminActionBean.class);
    @SpringBean
    AdministratorService adminService;
    private List<AdministratorDTO> admins;
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true),
        @Validate(on = {"add", "save"}, field = "description", required = true)
    })
    private AdministratorDTO admin;

    public AdministratorDTO getAdmin() {
        return admin;
    }

    public void setAdmin(AdministratorDTO admin) {
        this.admin = admin;
    }

    public Resolution add() {
        adminService.createAdministrator(admin);
        getContext().getMessages().add(new LocalizableMessage("admin.add.message", escapeHTML(admin.getName()), escapeHTML(admin.getDescription())));
        return new RedirectResolution(this.getClass(), "list");
    }

    @DefaultHandler
    public Resolution list() {
        admins = adminService.retrieveAllAdministrators();
        return new ForwardResolution("/admin/list.jsp");
    }

    public List<AdministratorDTO> getAdmins() {
        return admins;
    }

    public Resolution delete() {
        //only id is filled by the form
        admin = adminService.retrieveAdministratorById(admin.getId());
        System.out.println(admin.getId());
        adminService.deleteAdministrator(admin);
        getContext().getMessages().add(new LocalizableMessage("admin.delete.message",
                escapeHTML(admin.getName()), escapeHTML(admin.getDescription())));
        return new RedirectResolution(this.getClass(), "list");
    }

    @Override
    public Resolution handleValidationErrors(ValidationErrors errors) throws Exception {
        //fill up the data for the table if validation errors occured
        //return null to let the event handling continue
         admins = adminService.retrieveAllAdministrators();
        return null;
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadAdminFromDatabase() {
        String ids = getContext().getRequest().getParameter("admin.id");
        if (ids == null) {
            return;
        }
        admin = adminService.retrieveAdministratorById(Long.parseLong(ids));
    }

    public Resolution edit() {
        return new ForwardResolution("/admin/edit.jsp");
    }

    public Resolution save() {
        adminService.updateAdministrator(admin);
        return new RedirectResolution(this.getClass(), "list");
    }
}
